package com.example.ex_widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class Ex_GridView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex__grid_view);

        setTitle("网络视图");
        GridView gv = (GridView)findViewById(R.id.grid_view);
        gv.setAdapter(new ImageAdapter(this));
    }

    public class ImageAdapter extends BaseAdapter{
        private Context mContext;

        public ImageAdapter(Context c){
            mContext = c;
        }

        private Integer[] mThumbIds = {R.drawable.grid_view_01,
                R.drawable.grid_view_02, R.drawable.grid_view_03,
                R.drawable.grid_view_04,R.drawable.grid_view_05,};

        public int getCount(){
            return mThumbIds.length;
        }

        public Object getItem(int arg0){
            return null;
        }

        @Override
        public long getItemId(int arg0){
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            ImageView imageView;
            if(convertView == null){
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(200,200));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);
            }else{
                imageView = (ImageView)convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        menu.add(0,1,1, "返回上一页");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if (item.getItemId()==1){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}
